package com.atguigu.queue;

import java.util.Scanner;

public class MyQueueDemo {
    public static void main(String[] args) {
        //测试一把
        //创建一个队列
        MyQueue queue = new MyQueue(3);
        char key = ' '; //接收用户输入
        Scanner scanner = new Scanner(System.in);//
        boolean loop = true;
        //输出一个菜单
        while(loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);//接收一个字符
            switch (key) {
                case 's':
                    queue.printData();
                    break;
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();
                    queue.addData(value);
                    break;
                case 'g': //取出数据
                    try {
                        int res = queue.getData();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': //查看队列头的数据
                    try {
                        int res = queue.getHeadData();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e': //退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }

        System.out.println("程序退出~~");
    }
}

class MyQueue {
    public int maxSize;
    public int front;
    public int rear;
    public int[] arr;

    MyQueue(int maxSize) {
        this.maxSize=maxSize;
        arr=new int[3];
        front=0;
        rear=0;
    }
    public  boolean isEmpty(){
        return  front==rear;
    }
    public boolean isFull(){
        return (rear  + 1) % maxSize == front;

    }
    public void addData( int data){
        if (isFull()){
            System.out.println("队列已满，不能再添加");
            return;
        }
        arr[rear]=data;
        rear=(rear+1)%(maxSize);
    }
    public int getData() throws Exception {
        if(isEmpty()){
            throw new Exception("队列为空，请添加数据");
        }
        int a= arr[front];
        front=(front+1)%maxSize;
        return  a;

    }
    public void printData(){
        for (int i=front; i<size();i++){
            System.out.println(arr[i]);
        }
    }
    public int size() {
        // rear = 2
        // front = 1
        // maxSize = 3
        return (rear + maxSize - front) % maxSize;
    }
    public int  getHeadData(){
        return  arr[front];
    }
}